<?php
    session_start();
    $pag = 0;
    $nperpag = 5;
    function curPageURL() {
         $pageURL = 'http';
         if ($_SERVER["HTTPS"] == "on") {$pageURL .= "s";}
         $pageURL .= "://";
         if ($_SERVER["SERVER_PORT"] != "80") {
          $pageURL .= $_SERVER["SERVER_NAME"].":".$_SERVER["SERVER_PORT"].$_SERVER["REQUEST_URI"];
         } else {
          $pageURL .= $_SERVER["SERVER_NAME"].$_SERVER["REQUEST_URI"];
         }
         return $pageURL;
    }

    function getUserNamePage(){
        if (isset($_SESSION['username']))
	{
		$login = true;
	}
	else{
		$login = false;
		//header("Location: ".$CFG->url."home.php");
	}
	$loguname = $_SESSION['username'];
	$username = $_GET["un"];
	if ($username==NULL&&$login){
		$username=$loguname;
	}
        return $username;
    }
/*
    function getUserNamePage(){
        $url = curPageURL();
        $j = -1;
        $sleng = strlen($url);
        for ($i = 0; $i < $sleng; ++$i){
            if ($url[$i] == '?'){
                $j = $i + 1;
            }
        }

        if ($j != -1){
            return substr($url, $j);
        }
        else{
            return "";
        }

    }
*/
    function getUserNamePage2(){
        $url = curPageURL();
        $j = -1;
        $k = -1;
        $sleng = strlen($url);
        for ($i = 0; $i < $sleng; ++$i){
            if ($url[$i] == '?'){
                $j = $i + 1;
            }
            if ($url[$i] == '&'){
                $k = $i;
            }
        }

        if (($j != -1) && ($k > $j)){
            return substr($url, $j, $k - $j);
        }
        else if (($j != -1) && ($k == -1)){
            return substr($url, $j);
        }
        else{
            return "";
        }

    }

    function getIdThread(){
        $id = getIdThread2();
        if ($id != NULL)
	{
		$ada = true;
	}
	else{
		$ada = false;
	}
	$idthread = $_GET["id"];
	if ($idthread==NULL&&$ada){
		$idthread=$id;
	}
        return $idthread;
    }

    function getIdThread2(){
        $url = curPageURL();
        $k = -1;
        $sleng = strlen($url);
        for ($i = 0; $i < $sleng; ++$i){
            if ($url[$i] == '&'){
                $k = $i + 1;
            }
        }
        if ($k != -1){
            return substr($url, $k);
        }
        else{
            return "";
        }

    }
?>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
    <link rel="stylesheet" type="text/css" href="style/message.css" />
    <link rel="stylesheet" type="text/css" href="style/footoo.css" />
    <link rel="stylesheet" type="text/css" href="style/ftheader.css" />
    <link rel="stylesheet" type="text/css" href="style/ftfooter.css" />
    <link rel="stylesheet" type="text/css" href="style/ftcommoncontent.css" />
    <link rel="stylesheet" type="text/css" href="style/ftprofmes.css" />
    <link rel="icon" href="images/footoologomini.png" type="image/png" />
    <script type="text/javascript" src="script/search.js"></script> 
    <script type="text/javascript" src="script/visopt.js"></script>
    <script type="text/javascript" src="script/registrasi.js"></script>
    <script type="text/javascript" src="script/datetimepicker.js"></script>
    <title>Message</title>
    <script type="text/javascript">
        var stringnama = "";
        function check(count){
            if (document.getElementById("judularea").value.length == 0){
                document.getElementById("judularea").value = "NO SUBJECT";
            }
            
            if (document.getElementById("pesanarea").value.length == 0){
                alert("Anda belum mengisi pesan. Isi terlebih dahulu.");
            }
            else{
                for (var i = 0; i < count; ++i){
                    if (document.getElementById("checklist_" + i).checked){
                        stringnama = stringnama + " "  + document.getElementById("checklist_"+i).value;
                    }
                }
                stringnama = stringnama + " ";
                document.getElementById("stringuser").value = stringnama;

                document.forms["kirimpesan"].submit();
            }
        }

        function backtomessages(user){
            document.location = "message.php?un="+user;
        }

        function nextpes(n, user, max){
            if (n < max){
                ++n;
            }
            var xmlhttp;
            if (window.XMLHttpRequest){// code for IE7+, Firefox, Chrome, Opera, Safari
                xmlhttp=new XMLHttpRequest();
            }
            else{// code for IE6, IE5
                xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
            }
            xmlhttp.onreadystatechange=function(){
                if (xmlhttp.readyState==4 && xmlhttp.status==200){
                    document.getElementById("messagecontentajax2").innerHTML= xmlhttp.responseText;
                }
            }

            xmlhttp.open("GET","message2.php?un="+user+"&n="+n,true);
            xmlhttp.send();
        }

        function prevpes(n,user, max){
            if (n > 0){
                --n;
            }
            var xmlhttp;
            if (window.XMLHttpRequest){// code for IE7+, Firefox, Chrome, Opera, Safari
                xmlhttp=new XMLHttpRequest();
            }
            else{// code for IE6, IE5
                xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
            }
            xmlhttp.onreadystatechange=function(){
                if (xmlhttp.readyState==4 && xmlhttp.status==200){
                    document.getElementById("messagecontentajax2").innerHTML= xmlhttp.responseText;
                }
            }

            xmlhttp.open("GET","message2.php?un="+user+"&n="+n,true);
            xmlhttp.send();
        }

        function goToMessageDetail(user, id){
            var xmlhttp;
            if (window.XMLHttpRequest){// code for IE7+, Firefox, Chrome, Opera, Safari
                xmlhttp=new XMLHttpRequest();
            }
            else{// code for IE6, IE5
                xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
            }
            xmlhttp.onreadystatechange=function(){
                if (xmlhttp.readyState==4 && xmlhttp.status==200){
                    document.getElementById("contentwrapper").innerHTML= xmlhttp.responseText;
                }
            }

            xmlhttp.open("GET","messagedetail1.php?un="+user+"&id="+id,true);
            xmlhttp.send();
        }

        function backtomessages(user){
            var xmlhttp;
            if (window.XMLHttpRequest){// code for IE7+, Firefox, Chrome, Opera, Safari
                xmlhttp=new XMLHttpRequest();
            }
            else{// code for IE6, IE5
                xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
            }
            xmlhttp.onreadystatechange=function(){
                if (xmlhttp.readyState==4 && xmlhttp.status==200){
                    document.getElementById("contentwrapper").innerHTML= xmlhttp.responseText;
                }
            }

            xmlhttp.open("GET","message.php?un="+user,true);
            xmlhttp.send();
        }

        function sendpesan(){
            document.forms["kirimpesan"].submit();
        }

        function nextisipes(n, user, max, id){
            if (n < max){
                ++n;
            }
            var xmlhttp;
            if (window.XMLHttpRequest){// code for IE7+, Firefox, Chrome, Opera, Safari
                xmlhttp=new XMLHttpRequest();
            }
            else{// code for IE6, IE5
                xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
            }
            xmlhttp.onreadystatechange=function(){
                if (xmlhttp.readyState==4 && xmlhttp.status==200){
                    document.getElementById("messagedetailcontentajax2").innerHTML= xmlhttp.responseText;
                }
            }

            xmlhttp.open("GET","messagedetail2.php?un="+user+"&id="+id+"&n="+n,true);
            xmlhttp.send();
        }

        function previsipes(n,user, id){
            if (n > 0){
                --n;
            }
            var xmlhttp;
            if (window.XMLHttpRequest){// code for IE7+, Firefox, Chrome, Opera, Safari
                xmlhttp=new XMLHttpRequest();
            }
            else{// code for IE6, IE5
                xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
            }
            xmlhttp.onreadystatechange=function(){
                if (xmlhttp.readyState==4 && xmlhttp.status==200){
                    document.getElementById("messagedetailcontentajax2").innerHTML= xmlhttp.responseText;
                }
            }

            xmlhttp.open("GET","messagedetail2.php?un="+user+"&id="+id+"&n="+n,true);
            xmlhttp.send();
        }
    </script>
</head>

<body>
    <div id="headerwrapper">
	<div id = "headerbgfx"></div>
        <div id="footooheader">
            <div id="footoologo">
            	<a href="#"></a>
            </div>
            <div id="logprofpicarea">
            <?php require 'php/viewer/header.php'; ?>
            </div>
        </div>
        <div id="footoomenu">
            <ul>
                <li>
                    <a href="home.php">Home</a>
                </li>
                <li>
                    <?php
                        echo '<a href="profile.php?'.$_SESSION['username'].'">Profile</a>';
                    ?>
                </li>
                <li>
                    <?php
                        echo '<a href="wall.php?'.$_SESSION['username'].'">Wall</a>';
                    ?>
                </li>
                <li>
                    <?php
                        echo '<a href="fts.php" >Photos</a>';
                    ?>
                </li>
                <li>
                    <?php
                        echo '<a href="message.php?un='.$_SESSION['username'].'" class="current">Message</a>';
                    ?>
                </li>
            </ul>
        </div>
    </div>
    <div id="headermirror">
        <div id="searchbox">
            <form action="search.php" method="post">
            	<select class="searchselect" name="tipe">
                	<option value="user">User</option>
                	<option value="photo">Photo</option>
                </select>
                <div class="searchbutton">
                    <input id="searchbutton" type="submit" value="" />
                </div>
                <div class="searchtext">
                <input id="searchtext" class="searchtext" type="text" name="searchtext" value="search" onfocus="filledTrue(this.id, 'search')" onblur="filledFalse(this.id, 'search')" />
            	</div>
            </form>
        </div>
    </div>
    <div id="contentwrapper">
        <div class="boxy" id="messagearea">
            <div class="messagepagecontent">
                
                <div>
                    <p class="judul">Buat Pesan Baru</p>
                </div>

                <div id="messagecontentajax1">

                <?php
                    $user = $_SESSION['username'];
                    require_once('php/database.php');
                    $hasil1 = database::query("SELECT * FROM user WHERE Username = '$user'");
                    if(mysql_num_rows($hasil1)>0){
                        $baris1 = mysql_fetch_row($hasil1);
						$gambar = photo::getProfPicPath($user);
                    }

                    echo '<div class="box">';
                        echo '<form action="messagenewpost.php?un='.$user.'" method="post" name="kirimpesan">';
                        //echo '<form action="message2.php?un='.$user.'&n='.$pag.'" method="post" name="kirimpesan">';
                        echo '<table align="center">';
                            echo '<tr>';
                                    echo '<td>';
                                        echo '<img id="foto" src="'.$gambar.'" border="0" height="75" width="75"/>';
                                    echo '</td>';
                                    echo '<td id="pesansiap">';
                                        echo '<label style="font-size: 10px; float: left;">';
                                            echo 'Insert your message subject.';
                                        echo '</label>';
                                        echo '<div>';
                                            echo '<input type="text" class="textpesan" name="judularea" id="judularea"></input>';
                                        echo '</div>';
                                        echo '<label style="font-size: 10px; float: left;">';
                                            echo 'Insert your message below.';
                                        echo '</label>';
                                        echo '<div>';
                                            echo '<textarea class="textpesan" name="pesanarea" id="pesanarea"></textarea>';
                                        echo '</div>';
                                        
                                    echo '</td>';
                            echo '</tr>';
                        echo '</table>';

                        echo '<div align="center">';
                            echo '<br/><br/>';
                            echo '<label style="font-size: 10px; float: left; margin-left: 75px">';
                                echo 'Chosse recipient(s).<br/>';
                            echo '</label>';
                            echo '<table width="700">';
                                    echo '<tr>';
                                        echo '<td>';

                            $hasil3 = database::query("SELECT Follower FROM follownet WHERE Username = '$user' ");
                            $hasil4 = database::query("SELECT Username FROM follownet WHERE Follower = '$user' ");
                            $count = 0;
                            $arrusr = array(NULL);
                            if(mysql_num_rows($hasil3)>0){
                                while($baris3 = mysql_fetch_row($hasil3)){
                                    $usrnama = $baris3[0];
                                    $arrusr[$count] = $usrnama;
                                    echo '<input type="checkbox" class="usercheckbox" name="checklist_'.$count.'" id="checklist_'.$count.'" value="'.$usrnama.'">'.$usrnama.'</input>';
                                    
                                    if ((++$count % 2) == 0){
                                        echo '</td></tr><tr><td>';
                                    }
                                    else{
                                        echo '</td><td>';
                                    }
                                }
                            }
                            if(mysql_num_rows($hasil4)>0){
                                while($baris4 = mysql_fetch_row($hasil4)){
                                    $usrnama = $baris4[0];
                                    $arrusr[$count] = $usrnama;
                                    echo '<input type="checkbox" class="usercheckbox" name="checklist_'.$count.'" id="checklist_'.$count.'" value="'.$usrnama.'">'.$usrnama.'</input>';
                                    
                                    if ((++$count % 2) == 0){
                                        echo '</td></tr><tr><td>';
                                    }
                                    else{
                                        echo '</td><td>';
                                    }
                                }
                            }
                                            echo '<input type="hidden" value="'.$count.'" name="count" id="count"/>';
                                            echo '<input type="hidden" name="stringuser" id="stringuser"/>';

                                        echo '</td>';
                                    echo '</tr>';
                                    echo '<tr>';
                                        echo '<td>';
                                        echo '</td>';
                                        echo '<td>';
                                            /*echo '<div>';
                                                //echo '< type="button" style="float: right;" id="kirimpesan" onclick="check('.$count.')"/>';
                                                echo '<div class="button180" style="float: right;" onclick="check('.$count.')">Send</div>';
                                            echo '</div>';*/
                                        echo '</td>';
                                    echo '</tr>';
                            echo '</table>';
                            echo '<div>';
                                //echo '< type="button" style="float: right;" id="kirimpesan" onclick="check('.$count.')"/>';
                                echo '<div class="button180" onclick="check('.$count.')">Send</div>';
                            echo '</div>';
                        echo '</div>';
                        echo '</form>';
                    echo '</div>';
                ?>

                </div>
                <div id="messagecontentajax2">

                <?php
                    $user = $_SESSION['username'];
                    $hasil1 = database::query("SELECT IDThread FROM messagetarget WHERE Username = '$user'");
                    if(mysql_num_rows($hasil1)>0){
                        echo '<div id="daerahjudulpesan">';
                        echo '<p class="judul">Pilih Judul</p>';
                        $indeksArray = 0;
                        while ($baris1 = mysql_fetch_row($hasil1)){
                            $idthread = $baris1[0];

                            $hasil2 = database::query("SELECT JudulThread FROM thread WHERE IDThread = '$idthread'");
                            if(mysql_num_rows($hasil2)>0){
                                $baris2 = mysql_fetch_row($hasil2);
                                $judul = $baris2[0];

                            }

                            $hasil4 = database::query("SELECT * FROM message WHERE IDThread = '$idthread' ORDER BY TanggalKirim DESC");
                            if(mysql_num_rows($hasil4)>0){
                                $baris4 = mysql_fetch_row($hasil4);
                                $waktu = $baris4[3];
                                $sipengirim = $baris4[0];
                            }

                            $idArray[$indeksArray] = $idthread;
                            $judulArray[$indeksArray] = $judul;
                            $waktuArray[$indeksArray] = $waktu;
                            $sipengirimArray[$indeksArray] = $sipengirim;

                            ++$indeksArray;

                        }

                        if (($indeksArray % $nperpag) ==  0){
                            $ntemp = (($indeksArray - ($indeksArray % $nperpag)) / $nperpag);
                        }
                        else{
                            $ntemp = (($indeksArray - ($indeksArray % $nperpag)) / $nperpag) + 1;
                        }
                        if ($ntemp > 0){
                            $npage = $ntemp - 1;
                        }

                        for ($j = 0; $j < $nperpag; ++$j){
                            echo '<div class="judulpesan">';
                                $j2 = $pag * $nperpag + $j;
                                if ($j2 < $indeksArray){
                                    //echo '<a href="messagedetail.php?un='.$user.'&id='.$idArray[$j2].'">'.$judulArray[$j2].'<br/></a>';
                                    echo '<a href="javascript:void()" onclick="goToMessageDetail(\''.$user.'\',\''.$idArray[$j2].'\')">'.$judulArray[$j2].'<br/></a>';
                                    echo '<div>';
                                        echo '<label>';
                                            echo ''.$waktuArray[$j2].' by ';
                                        echo '</label>&nbsp';
                                        echo '<label>';
                                            echo ''.$sipengirimArray[$j2].'';
                                        echo '<br/></label>';
                                    echo '</div>';
                                }
                            echo '</div>';
                        }

                        echo '</div>';
                        echo '<div align="center">';
                            echo '<input type="button" id="prevpesan" name="prevpesan" value="prev" onclick="prevpes('.$pag.',\''.$user.'\','.$npage.')">';

                            echo '</input>';
                            echo '<input type="button" id="nextpesan" name="nextpesan" value="next" onclick="nextpes('.$pag.',\''.$user.'\','.$npage.')">';

                            echo '</input>';
                        echo '</div>';
                    }

                    else{
                        echo '<div id="daerahjudulpesan">';
                        echo '<p class="judul">Belum Ada Pesan</p>';
                        echo '</div>';
                    }

                ?>
                </div>
            </div>
        </div>
    </div>
    <div id="footerwrapper">
    	<div id="footer">
            <div id="footerleft"><br/>
            	ft, ft logoes are trademark of ft company. <br/>
            	Copyright 2010 by : <br/>
                13508011 - M. Albadr Lutan Nasution <br/>
                13508019 - Gregorius Ronny Kaluge <br/>
                13508099 - Alwi Alfiansyah Ramdan <br/>
            </div>
            <div id="footermiddle">

            </div>
            <div id="footerright">

            </div>
        </div>
    </div>
</body>
</html>

